<?php

namespace App\Admin\Controllers;

use App\Models\Coupon;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Coupon\Allot;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Extensions\Tools\CouponStatusGroup;

class CouponController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Coupon';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Coupon());
        if(isset($_GET['status']) && $_GET['status']!==''){
            $grid->model()->where('status',intval($_GET['status']));
        }
        $grid->column('id', __('Id'));
        $grid->column('code', __('Code'));
        $grid->column('batch_code', __('Batch code'));
        $grid->column('status', __('Status'));
        $grid->column('valid_type', __('Valid type'));
        $grid->column('valid_dates', __('Valid dates'));
        $grid->column('allot_time', __('Allot time'));
        $grid->column('accepter_phone', __('Accepter phone'));
        $grid->column('exchange_time', __('Exchange time'));
        $grid->column('exchange_user_id', __('Exchange user id'));
        $grid->column('exchange_order_id', __('Exchange order id'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->selector(function (Grid\Tools\Selector $selector) {
            $selector->select('status', '状态', [
                0 => '未分配',
                1 => '已分配',
                2 => '已预约',
                3 => '已完成',
                -1 => '已过期',
            ]);
        });

        $grid->actions(function ($actions) {
            $actions->add(new Allot);
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->equal('code', '卡号');
            $filter->equal('accepter_phone', '分配手机号');
            $filter->equal('allot_order_unicompay', '流水号');
        });

        $grid->tools(function ($tools) {
            $tools->append(new CouponStatusGroup());
        });
        

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Coupon::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('code', __('Code'));
        $show->field('batch_code', __('Batch code'));
        $show->field('status', __('Status'));
        $show->field('valid_type', __('Valid type'));
        $show->field('valid_dates', __('Valid dates'));
        $show->field('allot_time', __('Allot time'));
        $show->field('accepter_phone', __('Accepter phone'));
        $show->field('exchange_time', __('Exchange time'));
        $show->field('exchange_user_id', __('Exchange user id'));
        $show->field('exchange_order_id', __('Exchange order id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Coupon());

        $form->text('code', __('Code'));
        $form->text('batch_code', __('Batch code'));
        $form->switch('status', __('Status'));
        $form->switch('valid_type', __('Valid type'));
        $form->text('valid_dates', __('Valid dates'))->default('366d');
        $form->datetime('allot_time', __('Allot time'))->default(date('Y-m-d H:i:s'));
        $form->text('accepter_phone', __('Accepter phone'));
        $form->datetime('exchange_time', __('Exchange time'))->default(date('Y-m-d H:i:s'));
        $form->number('exchange_user_id', __('Exchange user id'));
        $form->number('exchange_order_id', __('Exchange order id'));

        return $form;
    }
}
